package org.jeecg.modules.basic.demo.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.IdGeneratorSnowflake;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.school.entity.LwgyBaseSchool;
import org.jeecg.modules.basic.demo.school.service.ILwgyBaseSchoolService;
import org.jeecg.modules.basic.demo.student.entity.ExportStudent;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.demo.student.mapper.LwgyBaseStudentMapper;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: lwgy_base_student
 * @Author: jeecg-boot
 * @Date: 2020-12-11
 * @Version: V1.0
 */
@Api(tags = "lwgy_base_student")
@RestController
@RequestMapping("/student/lwgyBaseStudent")
@Slf4j
public class LwgyBaseStudentController extends JeecgController<LwgyBaseStudent, ILwgyBaseStudentService> {
    @Autowired
    private ILwgyBaseStudentService lwgyBaseStudentService;

    @Autowired
    private ILwgyBaseSchoolService lwgyBaseSchoolService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IdGeneratorSnowflake snowflake;


    @Resource
    private LwgyBaseStudentMapper lwgyBaseStudentMapper;

    /**
     * 分页列表查询
     *
     * @param lwgyBaseStudent
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "lwgy_base_student-分页列表查询")
    @ApiOperation(value = "lwgy_base_student-分页列表查询", notes = "lwgy_base_student-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(LwgyBaseStudent lwgyBaseStudent,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<LwgyBaseStudent> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBaseStudent, req.getParameterMap());
        Page<LwgyBaseStudent> page = new Page<>(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        System.err.println("sysUser:"+sysUser);
        List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
        System.err.println("roles:"+roles);

        if (roles.contains("fdy")) {
//            queryWrapper.eq("student_teacher", sysUser.getWorkNo());
            List<String> classId = lwgyBaseStudentService.getClassIdBySysUser(sysUser);
            if (oConvertUtils.listIsNotEmpty((classId))) {
                queryWrapper.in("student_class", classId);
            } else {
                queryWrapper.eq("student_class", "null");
            }
        }
       /* if (roles.contains("yanjiusheng")) {
            System.err.println("走这里了！！！");
            String eduCation=sysUser.getSelectedEducation();
            System.err.println("classId:::"+ eduCation);
            String[] fields = eduCation.split(",");
            System.err.println("fields"+fields);
           *//* List<String> classId = lwgyBaseStudentMapper.getAllSysUsers(eduCation);*//*
           *//* System.err.println("classId"+classId);*//*
            queryWrapper.in("student_education",Arrays.asList(fields));
            queryWrapper.eq("student_state", 0);

        }*/
        if (roles.contains("yanjiusheng")) {
            System.err.println("走这里了！！！");
            String eduCation = sysUser.getSelectedEducation();

            // 去掉中括号和引号
            String cleanedEducation = eduCation.replace("[", "").replace("]", "").replace("\"", "");


            String[] fields = cleanedEducation.split(",");
            System.err.println("fieldsss" + Arrays.toString(fields));

            queryWrapper.in("student_education", Arrays.asList(fields));
            queryWrapper.eq("student_state", 0);
        }

        IPage<LwgyBaseStudent> pageList = lwgyBaseStudentService.queryPageListStudent(page, queryWrapper);
        return Result.OK(pageList);
    }
    /**
     * 分页列表查询
     *
     * @param lwgyBaseStudent
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "lwgy_base_student-分页列表查询")
    @ApiOperation(value = "lwgy_base_student-分页列表查询", notes = "lwgy_base_student-分页列表查询")
    @GetMapping(value = "/list1")
    public Result<?> queryLeaveList(LwgyBaseStudent lwgyBaseStudent,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<LwgyBaseStudent> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBaseStudent, req.getParameterMap());
        Page<LwgyBaseStudent> page = new Page<>(pageNo, pageSize);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());

        if (roles.contains("fdy")) {
            List<String> classId = lwgyBaseStudentService.getClassIdBySysUser(sysUser);
            if (oConvertUtils.listIsNotEmpty((classId))) {
                queryWrapper.in("student_class", classId);
            } else {
                queryWrapper.eq("student_class", "null");
            }
        }
        if (roles.contains("yanjiusheng")) {
            String eduCation = sysUser.getSelectedEducation();
            // 去掉中括号和引号
            String cleanedEducation = eduCation.replace("[", "").replace("]", "").replace("\"", "");
            String[] fields = cleanedEducation.split(",");
            queryWrapper.in("student_education", Arrays.asList(fields));
            queryWrapper.eq("student_state", 0);
        }
        String studentState = lwgyBaseStudent.getStudentState();
        IPage<LwgyBaseStudent> pageList = lwgyBaseStudentService.queryPageLeaveStudent(page, queryWrapper,studentState);
        return Result.OK(pageList);
    }

    /**
     * 自定义分页列表查询
     *
     * @param lwgyBaseStudent
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "lwgy_base_student-自定义分页列表查询")
    @ApiOperation(value = "lwgy_base_student-自定义分页列表查询", notes = "lwgy_base_student-自定义分页列表查询")
    @GetMapping(value = "/customerList")
    public Result<?> queryCustomerList(LwgyBaseStudent lwgyBaseStudent,
                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                       HttpServletRequest req) {
        System.out.println("lwgyBaseStudent =======>>>>> " + lwgyBaseStudent);
        QueryWrapper<LwgyBaseStudent> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBaseStudent, req.getParameterMap());
        Page<LwgyBaseStudent> page = new Page<>(pageNo, pageSize);
        IPage<LwgyBaseStudent> pageList = lwgyBaseStudentService.queryPageCustomer(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param lwgyBaseStudent
     * @return
     */
    @AutoLog(value = "lwgy_base_student-添加")
    @ApiOperation(value = "lwgy_base_student-添加", notes = "lwgy_base_student-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody LwgyBaseStudent lwgyBaseStudent) {

        /*String studentClass = lwgyBaseStudent.getStudentClass();
        if (oConvertUtils.isNotEmpty(studentClass)) {
            LwgyBaseSchool lwgyBaseSchool = lwgyBaseSchoolService.getById(studentClass);
            if (oConvertUtils.isNotEmpty(lwgyBaseSchool)) {
                String universityCode = lwgyBaseSchool.getUniversityCode();
                if (oConvertUtils.isNotEmpty(universityCode)) {
                    StringBuilder username = new StringBuilder();
                    List<SysUser> sysUserList = lwgyBaseSchoolService.getUserByUniversityCode(universityCode);
                    for (SysUser sysUser : sysUserList) {
                        username.append(sysUser.getWorkNo()).append(",");
                    }
                    if (oConvertUtils.isNotEmpty(username)) {
                        String strUsername = username.substring(0, username.length() - 1);
                        lwgyBaseStudent.setStudentTeacher(strUsername);
                    }
                }
            }
        }*/
        lwgyBaseStudentService.save(lwgyBaseStudent);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param lwgyBaseStudent
     * @return
     */
    @AutoLog(value = "lwgy_base_student-编辑")
    @ApiOperation(value = "lwgy_base_student-编辑", notes = "lwgy_base_student-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody LwgyBaseStudent lwgyBaseStudent) {
        lwgyBaseStudentService.updateById(lwgyBaseStudent);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "lwgy_base_student-通过id删除")
    @ApiOperation(value = "lwgy_base_student-通过id删除", notes = "lwgy_base_student-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        lwgyBaseStudentService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "lwgy_base_student-批量删除")
    @ApiOperation(value = "lwgy_base_student-批量删除", notes = "lwgy_base_student-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.lwgyBaseStudentService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "lwgy_base_student-通过id查询")
    @ApiOperation(value = "lwgy_base_student-通过id查询", notes = "lwgy_base_student-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        LwgyBaseStudent lwgyBaseStudent = lwgyBaseStudentService.getById(id);
        if (lwgyBaseStudent == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(lwgyBaseStudent);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param lwgyBaseStudent
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyBaseStudent lwgyBaseStudent) {
        // Step.1 组装查询条件
        QueryWrapper<LwgyBaseStudent> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBaseStudent, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
        if (roles.contains("fdy")) {
//            queryWrapper.eq("student_teacher", sysUser.getWorkNo());
            List<String> classId = lwgyBaseStudentService.getClassIdBySysUser(sysUser);
            if (oConvertUtils.listIsNotEmpty((classId))) {
                queryWrapper.in("student_class", classId);
            } else {
                queryWrapper.eq("student_class", "null");
            }
        }

        // Step.2 获取导出数据
        List<LwgyBaseStudent> pageList = lwgyBaseStudentMapper.queryPageListCustom(null, queryWrapper);
        List<ExportStudent> exportStudents = new ArrayList<>(pageList.size());
        ExportStudent exportStudent;
        for (LwgyBaseStudent baseStudent : pageList) {
            exportStudent = new ExportStudent();
            BeanUtils.copyProperties(baseStudent, exportStudent);
            exportStudents.add(exportStudent);
        }

        List<ExportStudent> exportList = null;

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = exportStudents.stream().filter(item -> selectionList.contains(getId(item))).collect(Collectors.toList());
        } else {
            exportList = exportStudents;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "lwgy_base_student"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, ExportStudent.class);
//        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("学生信息", "导出人:" + sysUser.getRealname(), "学生信息表"));
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("学生信息", ""));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
    /**
     * 导出离校excel
     *
     * @param request
     * @param lwgyBaseStudent
     */
    @RequestMapping(value = "/exportXlsLx")
    public ModelAndView exportXlsLx(HttpServletRequest request, LwgyBaseStudent lwgyBaseStudent) {
        // Step.1 组装查询条件
        QueryWrapper<LwgyBaseStudent> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBaseStudent, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
        if (roles.contains("fdy")) {
//            queryWrapper.eq("student_teacher", sysUser.getWorkNo());
            List<String> classId = lwgyBaseStudentService.getClassIdBySysUser(sysUser);
            if (oConvertUtils.listIsNotEmpty((classId))) {
                queryWrapper.in("student_class", classId);
            } else {
                queryWrapper.eq("student_class", "null");
            }
        }
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String leaveTime = "";
        if(month>9){
            //明年毕业生
            year = year+1;
            leaveTime = year+"-09-01 00:00:00";
        }else{
            //今年以及之前的毕业生
            year = year;
            leaveTime = year+"-09-01 00:00:00";
        }
        queryWrapper.isNotNull("stu.id");
        queryWrapper.lt("student_leaveschool",leaveTime);
        // Step.2 获取导出数据

        List<LwgyBaseStudent> pageList =lwgyBaseStudentMapper.queryPageListCustom(null, queryWrapper);
//        List<LwgyBaseStudent> pageList = lwgyBaseStudentMapper.queryPageListCustom(null, queryWrapper);
        List<ExportStudent> exportStudents = new ArrayList<>(pageList.size());
        ExportStudent exportStudent;
        for (LwgyBaseStudent baseStudent : pageList) {
            exportStudent = new ExportStudent();
            BeanUtils.copyProperties(baseStudent, exportStudent);
            exportStudents.add(exportStudent);
        }

        List<ExportStudent> exportList = null;

        // 过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = exportStudents.stream().filter(item -> selectionList.contains(getId(item))).collect(Collectors.toList());
        } else {
            exportList = exportStudents;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "lwgy_base_student"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, ExportStudent.class);
//        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("学生信息", "导出人:" + sysUser.getRealname(), "学生信息表"));
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("学生信息", ""));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
    /**
     * 导出学生excel模板
     *
     * @param request
     * @param lwgyBaseStudent
     */
    @RequestMapping(value = "/exportXlsStu")
    public ModelAndView exportXlsStu(HttpServletRequest request, LwgyBaseStudent lwgyBaseStudent) {
        // Step.1 组装查询条件
        QueryWrapper<LwgyBaseStudent> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBaseStudent, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
        if (!roles.contains("admin")) {
            queryWrapper.eq("student_teacher", sysUser.getWorkNo());
        }
        // Step.2 获取导出数据
        List<LwgyBaseStudent> pageList = lwgyBaseStudentMapper.queryPageListCustom(null, queryWrapper);
        List<ExportStudent> exportStudents = new ArrayList<>(1);
        ExportStudent exportStudent = new ExportStudent();
        exportStudent.setStudentNumber("202182080040(必填)");
        exportStudent.setStudentName("姓名(必填)");
        exportStudent.setStudentIdcard("身份证号(必填)");
        exportStudent.setStudentAddress("户籍");
        exportStudent.setStudentSex("男(必填)");
        exportStudent.setStudentEducation("学历(必填)");
        exportStudent.setStudentSchoolSystem("3(必填)");
        exportStudent.setStudentInschool(new Date());
        exportStudent.setStudentLeaveschool(new Date());
        exportStudent.setStudentState("学生状态");
        exportStudent.setStudentPhoto("照片");
        exportStudent.setStudentPhone("电话");
        exportStudent.setStudentDelflag("是否删除");
        exportStudent.setStudentTeacher("辅导员");
        exportStudent.setCampusName("青岛校区(必填)");
        exportStudent.setCollegeName("文法学院(必填)");
        exportStudent.setMajorName("汉语言文学(必填)");
        exportStudent.setYearName("2021(必填)");
        exportStudent.setClassName("班级");
        exportStudents.add(exportStudent);
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "学生信息"); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, ExportStudent.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("学生信息", ""));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportStudents);
        return mv;
    }
    public String getId(ExportStudent item) {
        try {
            return PropertyUtils.getProperty(item, "id").toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        MultipartFile file = fileMap.get("file");
        try {
            long start = System.currentTimeMillis();
            ImportParams params = new ImportParams();
            params.setNeedSave(true);
            params.setTitleRows(1);// 设置第一行为标题
            params.setHeadRows(2);// 设置第一行为标题
            List<LwgyBaseStudent> list = ExcelImportUtil.importExcel(file.getInputStream(), LwgyBaseStudent.class, params);
            List<String> stuNumber = new ArrayList<>(list.size());
            list.stream().forEach(item -> stuNumber.add(item.getStudentNumber()));//取出学生的学号
            QueryWrapper<LwgyBaseStudent> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id", "student_number");
            queryWrapper.in("student_number", stuNumber);
            List<LwgyBaseStudent> lwgyBaseStudentsNum = lwgyBaseStudentService.getBaseMapper().selectList(queryWrapper);//找出数据库中已经存在的学生
            lwgyBaseStudentsNum.forEach(lwgyBaseStudent -> {
                list.stream()
                        .filter(item -> item.getStudentNumber().equals(lwgyBaseStudent.getStudentNumber()))
                        .forEach(item -> item.setId(lwgyBaseStudent.getId()));
            });
            Iterator<LwgyBaseStudent> iterator = list.iterator();
            ArrayList<LwgyBaseStudent> outlist = new ArrayList<>();
            List<LwgyBaseStudent> lwgyBaseStudents = new ArrayList<>(list.size());
            while (iterator.hasNext()) {
                LwgyBaseStudent lwgyBaseStudent = iterator.next();
                String studentNumber = lwgyBaseStudent.getStudentNumber();
                String studentName = lwgyBaseStudent.getStudentName();
                String studentIdcard = lwgyBaseStudent.getStudentIdcard();
                String studentSex = lwgyBaseStudent.getStudentSex();
                String studentCampus = lwgyBaseStudent.getStudentCampus();
                String studentCollege = lwgyBaseStudent.getStudentCollege();
                String studentMajor = lwgyBaseStudent.getStudentMajor();
                String studentClass = lwgyBaseStudent.getStudentClass();
                String studentYear = lwgyBaseStudent.getStudentYear();
                if (oConvertUtils.isEmpty(studentNumber)) {
//                    lwgyBaseStudent.setStudentNumber("学号不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentName)) {
//                    lwgyBaseStudent.setStudentName("姓名不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentIdcard)) {
//                    lwgyBaseStudent.setStudentIdcard("身份证号码不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentSex)) {
//                    lwgyBaseStudent.setStudentSex("姓别不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentCampus)) {
//                    lwgyBaseStudent.setStudentCampus("校区不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentCollege)) {
//                    lwgyBaseStudent.setStudentCollege("学院不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentYear)) {
//                    lwgyBaseStudent.setStudentCollege("学院不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentMajor)) {
//                    lwgyBaseStudent.setStudentMajor("专业不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentClass)) {
//                    lwgyBaseStudent.setStudentClass("班级不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                } else if (oConvertUtils.isEmpty(studentYear)) {
//                    lwgyBaseStudent.setStudentYear("年级不能为空");
                    outlist.add(lwgyBaseStudent);
                    iterator.remove();
                }
                String key = new StringBuilder()
                        .append(studentCampus).append("_")
                        .append(studentCollege).append("_")
                        .append(studentYear).append("_")
                        .append(studentMajor).append("_")
                        .append(studentClass).append(":").toString();
                LwgyBaseSchool lwgyBaseSchool = (LwgyBaseSchool) redisUtil.get(key);//获取缓存中的classId
                if (oConvertUtils.isEmpty(lwgyBaseSchool)) {//缓存中不存在
                    lwgyBaseSchool = lwgyBaseSchoolService.getClassIdByName(lwgyBaseStudent);//从数据库获取对应的
                    redisUtil.set(key, lwgyBaseSchool, 180);//并将classId放入缓存
                }
                if (oConvertUtils.isNotEmpty(lwgyBaseSchool)) {
                    lwgyBaseStudent.setStudentClass(lwgyBaseSchool.getClassId());
                    lwgyBaseStudent.setStudentMajor(lwgyBaseSchool.getMajorId());
                    lwgyBaseStudent.setStudentYear(lwgyBaseSchool.getGradeId());
                    lwgyBaseStudent.setStudentCollege(lwgyBaseSchool.getFacultyId());
                    lwgyBaseStudent.setStudentCampus(lwgyBaseSchool.getCampusId());
                    lwgyBaseStudents.add(lwgyBaseStudent);
                } else {
                    lwgyBaseStudent.setStudentClass(lwgyBaseStudent.getStudentClass() + ":机构中未找到该班级");
                    lwgyBaseStudent.setStudentMajor(lwgyBaseStudent.getStudentMajor() + ":机构中未找到该专业");
                    lwgyBaseStudent.setStudentYear(lwgyBaseStudent.getStudentYear() + ":机构中未找到该年级");
                    lwgyBaseStudent.setStudentCollege(lwgyBaseStudent.getStudentCollege() + ":机构中未找到该学院");
                    outlist.add(lwgyBaseStudent);
                    //iterator.remove();
                }

            }
            long end = System.currentTimeMillis();

            if (outlist.size() == 0) {
                lwgyBaseStudentService.saveOrUpdateBatch(list);
                Map paramsMsg = new HashMap();
                paramsMsg.put("message", "成功导入" + list.size() + "条数据,共耗时:" + (end - start) / 1000 + "秒");
                return Result.OK(paramsMsg);
            } else {
                lwgyBaseStudentService.saveOrUpdateBatch(list);
                Map paramsMsg = new HashMap();
                paramsMsg.put("message", "成功导入" + list.size() + "条数据，共耗时:" + (end - start) / 1000 + "秒，存在" + outlist.size() + "条问题数据，是否导出？");
                paramsMsg.put("data", outlist);
                return Result.OK(paramsMsg);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("导入失败");
        }
    }

    /**
     * 根据部门id查询用户信息
     *
     * @param id
     * @return
     *///queryStuByNum
    @RequestMapping(value = "/queryStudentByScInfoId", method = RequestMethod.GET)
    public Result<?> queryStudentByScInfoId(@RequestParam(name = "id", required = true) String id,
                                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(name = "studentSex", required = false) String studentSex,
                                            String studentNumber, @RequestParam(name = "name", required = false) String name) {

        if (oConvertUtils.isNotEmpty(studentNumber)) {
            studentNumber = studentNumber.trim();
        }
        LwgyBaseSchool lwgyBaseSchool = lwgyBaseSchoolService.getById(id);
        Page<LwgyBaseStudent> page = new Page<>(pageNo, pageSize);
        IPage<LwgyBaseStudent> userList = lwgyBaseStudentService.queryStudentByCode(page, lwgyBaseSchool, studentSex, studentNumber);
        return Result.OK(userList);
    }

    /**
     * 根据学号查询学生
     *
     * @param lwgyBaseStudent
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "根据学号查询学生")
    @ApiOperation(value = "根据学号查询学生", notes = "根据学号查询学生")
    @RequestMapping("/queryStuByNum")
    public Result<?> queryStuByNum(LwgyBaseStudent lwgyBaseStudent,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   @RequestParam(name = "studentName", required = false) String studentName,
                                   @RequestParam(name = "studentNumber", required = false) String studentNumber,
                                   @RequestParam(name = "isBed", required = false) String isBed) {
        QueryWrapper<LwgyBaseStudent> queryWrapper = new QueryWrapper<>();
        if (oConvertUtils.isNotEmpty(lwgyBaseStudent.getStudentNumber())) {
            queryWrapper.likeRight("s.student_number", lwgyBaseStudent.getStudentNumber());
        }
        if (oConvertUtils.isNotEmpty(lwgyBaseStudent.getStudentName())) {
            queryWrapper.likeRight("s.student_name", lwgyBaseStudent.getStudentName());
        }
        if (oConvertUtils.isNotEmpty(lwgyBaseStudent.getIsBed())) {
            queryWrapper.having("isBed = " + isBed);
        }
        queryWrapper.eq("student_sex", lwgyBaseStudent.getStudentSex());
        Page<LwgyBaseStudent> page = new Page<>(pageNo, pageSize);
        IPage<LwgyBaseStudent> lwgyBaseStudentIPage = lwgyBaseStudentService.queryStuByNum(page, queryWrapper);
        return Result.OK(lwgyBaseStudentIPage);
    }


    /**
     * 查询所有学校
     */
    @AutoLog(value = "学校信息-通过pid查询")
    @ApiOperation(value = "学校信息-通过pid查询", notes = "学校信息-通过pid查询")
    @GetMapping(value = "/getSchoolData")
    public Result<?> getSchoolData(@RequestParam(name = "pid") String pid) {
        System.out.println("pid ------>>>> " + pid);
        List<Map<String, String>> maps = lwgyBaseStudentService.listByPid(pid);
        maps.forEach(System.out::println);
        return Result.OK(maps);
    }

    /**
     * 查询所有学校
     */
    @AutoLog(value = "学校信息-通过name查询")
    @ApiOperation(value = "学校信息-通过name查询", notes = "学校信息-通过name查询")
    @GetMapping(value = "/getSchoolName")
    public Result<?> getSchoolName(@RequestParam(name = "name") String name) {
        System.out.println("name ------>>>> " + name);
        List<Map<String, String>> maps = lwgyBaseStudentService.listByName(name);
        maps.forEach(System.out::println);
        return Result.OK(maps);
    }

    /**
     * 查询所有学校
     */
    @AutoLog(value = "学校信息-通过pid查询")
    @ApiOperation(value = "学校信息-通过pid查询", notes = "学校信息-通过pid查询")
    @GetMapping(value = "/getSchoolData2")
    public Result<?> getSchoolData2(@RequestParam(name = "pid") String pid) {
        System.out.println("pid ------>>>> " + pid);
        List<Map<String, String>> maps = lwgyBaseStudentService.listByPid2(pid);
        maps.forEach(System.out::println);
        return Result.OK(maps);
    }

   /* @AutoLog(value = "创建学生账号")
    @ApiOperation(value = "创建学生账号", notes = "创建学生账号")
    @PostMapping(value = "/generateStuAccount")
    public Result<?> generateStuAccount() {
        long start = System.currentTimeMillis();
        Integer count = lwgyBaseStudentService.generateStuAccount();
        long end = System.currentTimeMillis();
        return Result.OK("成功创建"+count+"条数据,共耗时(毫秒):" + (end - start), null);
    }*/
}



